﻿using System;
using System.Deployment.Application;
using System.Reflection;
using System.Windows.Forms;

namespace Y2M.Components
{
    public static class Utility
    {
        public static string Ellipse(this string text, int maxLength)
        {
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            if (text.Length <= maxLength)
                return text;

            return text.Substring(0, maxLength) + "...";
        }

        public static T Clone<T>(this T source, T clone) //where T : new()
        {
            foreach (PropertyInfo pi in source.GetType().GetProperties())
                if (pi.CanRead && pi.CanWrite)
                    pi.SetValue(clone, pi.GetValue(source, null), null);

            return clone;
        }

        public static void InvokeIfNeeded(this Control control, Action action)
        {
            if (control.InvokeRequired)
                control.Invoke(action);
            else
                action();
        }

        public static void InvokeIfNeeded<T>(this Control control, Action<T> action, T arg)
        {
            if (control.InvokeRequired)
                control.Invoke(action, arg);
            else
                action(arg);
        }

        public static void InvokeIfNeeded<T1, T2>(this Control control, Action<T1, T2> action, T1 arg1, T2 arg2)
        {
            if (control.InvokeRequired)
                control.Invoke(action, arg1, arg2);
            else
                action(arg1, arg2);
        }

        public static void InvokeIfNeeded<T1, T2, T3>(this Control control, Action<T1, T2, T3> action, T1 arg1, T2 arg2,
                                                      T3 arg3)
        {
            if (control.InvokeRequired)
                control.Invoke(action, arg1, arg2, arg3);
            else
                action(arg1, arg2, arg3);
        }

        public static void InvokeIfNeeded<T1, T2, T3, T4>(this Control control, Action<T1, T2, T3, T4> action, T1 arg1,
                                                          T2 arg2, T3 arg3, T4 arg4)
        {
            if (control.InvokeRequired)
                control.Invoke(action, arg1, arg2, arg3, arg4);
            else
                action(arg1, arg2, arg3, arg4);
        }

        public static Version GetVersion()
        {
            Version version = Assembly.GetEntryAssembly().GetName().Version;
            if (ApplicationDeployment.IsNetworkDeployed)
                version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
            return version;
        }

        public static string GetVersionText()
        {
            Version version = GetVersion();
            return String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Revision, version.Build);
        }

        public static string GetPublishVersionText()
        {
            Version version = null;

            try
            {
                version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
            }
            catch
            {
                version = GetVersion();
            }

            return String.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Revision);
        }
    }
}